<template>
	<view class="assetMain">
		<view class="assetList">
			<view class="asset-title">
				<view class="v-m">资产</view>
				<view class="v-m right itemHeadState">领用</view>
			</view>
			<view>申请单位：{{applyInfo.applyUnitName||'--'}}</view>
			<view>申请编号：{{applyInfo.assetApplyNo||'--'}}</view>
			<view>申请人：{{applyInfo.applicantName||'--'}}</view>
			<view>申请时间：{{applyInfo.applyTime||'--'}}</view>
			<view class="assetListItem" v-for="(item,index) in assetsList" :key="item.assetExecDtlId">
				<view class="itemContent">
					<view class="conCenter">
						<view style="font-weight: 600;">资产名称：{{item.assetName}}</view>
						<view>规格型号：{{item.specification}}</view>
						<view>资产类型：{{item.assetTypeName?item.assetTypeName:'--'}}</view>
						
						<view>资产编号：{{item.assetNo}}</view>
						<view style="display: flex;">
							申请单位：<view>{{item.applyUnitName}}</view>
						</view>
						
						<view>实施类型：{{item.execTypeCh}}</view>
						<view>实施人：{{item.executorName?item.executorName:'--'}}</view>
					</view>
					<view class="item-staus btn-orange-s" v-if="item.status==6">{{item.statusCh}}</view>
					<view class="item-staus btn-blue-s" v-if="item.status==5||item.status==8">{{item.statusCh}}</view>
					<view class="item-staus btn-orange-s" v-if="item.status==7">{{item.statusCh}}</view>
					<!-- <view class="itemHand" v-if="item.status !== 8">
						<view :class="{'color-g-bg':true,'permission-none':isPermission('ASS_IMP_DTL_QUERY')}" v-if="item.status==5&&item.execType==1" @click="execOutBtn(item)"><text class="iconfont icon-zichanguanli-zichantiaobo font-12"></text>&nbsp;&nbsp;调拨出库</view>
						<view :class="{'color-g-bg':true,'permission-none':isPermission('ASS_IMP_IMP')}" v-if="(item.status==5||item.status==6)&&(item.execType!=1)" @click="execBtn(item)"><text class="iconfont icon-baoxiu-xuanzhong font-12"></text>&nbsp;&nbsp;实施</view>
						<view :class="{'color-y-bg':true,'permission-none':isPermission('ASS_IMP_CFM')}" v-if="item.status==7" @click="signBtn(item)"><uni-icons type="compose" size="16" color="#fff"></uni-icons>&nbsp;&nbsp;签收</view>
					</view> -->
					<view class="itemHand" v-if="item.status == 6||item.status ==7">
						<view class="color-g-bg" @click="signBtn(item)">
							验收通过
						</view>
						<view class="color-g-bg" @click="noCompleteBtn(item)">
							未完成
						</view>
					</view>
				</view>
				
				
			</view>
		
		</view>
		<!-- 加载结束 -->
		<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
		<MessageTip ref="messageTip" :tipContent="tipContent" @clickType="handleShowOrHideTip"/>
	</view>
</template>

<script lang="ts">
	import {
		onPullDownRefresh,
		onReachBottom,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		defineComponent,
		ref,
		reactive
	} from 'vue'
	import {
		retrueData,
		exec,
		execOut,
		assetSign,
		applyDetailList
	} from '@/common/api/assetAPI.ts'
	import {
		CONSTANT
	} from '@/common/config/config.ts'
	import {
		fromSearch
	} from '@/common/util/tabSearchUse.ts'
	import {isPermission} from '@/common/util/common.ts'

	export default defineComponent({
		setup(props, {
			emit
		}) {
			const tipContent = ref('') //提示信息
			const messageTip = ref()
			const showLoadMore = ref(false);
			const loadMoreText = ref('加载中……'); //加载更多提示
			const assetsList = ref([]); //资产列表数据
			const applyInfo = ref({});
			const page = ref(CONSTANT.initPage); //初始加载页码
			const allPages = ref(0); //总页码数
			onPullDownRefresh(() => {
				page.value = CONSTANT.initPage; //重置页码
				assetsList.value = []; //重置列表
				getList();
			});
			onReachBottom(() => {
				//触发底部
				if (page.value > allPages.value || page.value == allPages.value) {
					loadMoreText.value = '没有更多的数据了'
					showLoadMore.value = true;
				} else {
					page.value++;
					loadMoreText.value = '加载中……';
					showLoadMore.value = true;
					getList();
				}

			})
			//获取列表数据
			const searchParams = ref({
				status: "5,6,7,8,9",
				assetApplyType: 1,
				mainApplyId: null,
				assetApplyId: null,
				assetExecDtlId:null,
			}); //查询条件
			const matchRule = {
				status: 'in',
				assetApplyType: 'eq',
				mainApplyId: 'eq',
				assetApplyId: 'eq',
				assetExecDtlId:'eq',
			}; //匹配条件

			onLoad((par) => {
				if (par.assetApplyId) {
					searchParams.value.mainApplyId = par.assetApplyId
				}
				if (par.byAssetApplyId) {
					searchParams.value.assetApplyId = par.byAssetApplyId
				}
				// 1 待提交 2 待审核3 审核中 4 审核不通过 5 待处理6 处理中7 待确认 8 已确认 9 确认不通过 10 已作废
				// if(par.status){
				// 	searchParams.value.status = par.status;//8 已确认
				// } else {
				// 	searchParams.value.status = '5,6,7';//状态为待处理或者处理中的
				// }
					getList();
				
			})

			function getList() {
				let config = {
					page: page.value,
					rows: CONSTANT.pageSize
				}
				const params = Object.assign(fromSearch(searchParams.value, matchRule), config)
				if(!isPermission('ASS_AAS_LIST_QUERY')){
					loadMoreText.value = '加载中...'
					retrueData(params).then((res: any) => {
						uni.stopPullDownRefresh(); //关闭下拉
						if (res.code === 0) {
							// console.log('----------',res.data);
							allPages.value = res.data.pages;
							page.value = res.data.current;
							let list: any[] = res.data.records;
							assetsList.value = assetsList.value.concat(list);
							if(assetsList.value.length>0){
									applyInfo.value = assetsList.value[0];
									if(applyInfo.value.applyTime){
										applyInfo.value.applyTime = applyInfo.value.applyTime.replace('T'," ");
									}
							}
							if (assetsList.value.length === 0) {
								showLoadMore.value = true;
								loadMoreText.value = '暂无数据'
							} else if (assetsList.value.length < CONSTANT.pageSize) {
								showLoadMore.value = true;
								loadMoreText.value = '没有更多的数据了'
							} else {
								showLoadMore.value = false;
							}
						}
					
					})
				}
				
			}
			function getDetList() {
				let config = {
					page: page.value,
					rows: CONSTANT.pageSize
				}
				const params = Object.assign(fromSearch(searchParams.value, matchRule), config)
				applyDetailList(params).then((res: any) => {
					uni.stopPullDownRefresh(); //关闭下拉
					if (res.code === 0) {
						allPages.value = res.data.pages;
						page.value = res.data.current;
						let list: any[] = res.data.records;
						assetsList.value = assetsList.value.concat(list);
						if(assetsList.value.length>0){
								applyInfo.value = assetsList.value[0];
						}
						if (assetsList.value.length === 0) {
							showLoadMore.value = true;
							loadMoreText.value = '暂无数据'
						} else if (assetsList.value.length < CONSTANT.pageSize) {
							showLoadMore.value = true;
							loadMoreText.value = '没有更多的数据了'
						} else {
							showLoadMore.value = false;
						}
					}
			
				})
			}

			// 实施
			const execParams = ref()
			const execBtn = (item) => {
				execParams.value=item
				tipContent.value = '确认资产实施？'
				messageTip.value.openTip()
			
			}
			//签收
			const signParams = ref()
			const signParams2 = ref()
			const signBtn = (item) =>{
				signParams.value=item
				tipContent.value='确认已到货？'
				messageTip.value.openTip()
				
			}
			

			// 调拨出库
			const execOutBtn = (item) => {
				execOut({
					assetExecDtlId: item.assetExecDtlId,
					assetNo: item.assetNo,
					version: item.version
				}).then(value => {
					if (value.code == 0) {
						uni.showToast({
							icon:'none',
							title:value.message,
							duration:500
						})
						setTimeout(()=>{
							assetsList.value = []
							getList();
						},500)
						
					} else {
						uni.showToast({
							icon:'none',
							title:value.message
						})
					}
				})
			}
			const handleShowOrHideTip = (type:string)=>{
					if(type=='yes'){
						if(tipContent.value=='确认资产实施？'){
							exec({
								...execParams.value
							}).then(value => {
								if (value.code == 0) {
											uni.showToast({
												title: value.message,
												icon:"none",
												duration:500
											});
											setTimeout(()=>{
												page.value = CONSTANT.initPage; //重置页码
												assetsList.value = []; //重置列表
													getList()
											},500)
								} else {
									uni.showToast({
										title: value.message,
										icon:"none"
									});
								}
							})
						}else if(tipContent.value=='确认已到货？'){
							assetSign([{
								assetExecDtlId:signParams.value.assetExecDtlId,
								version:signParams.value.version,
								status:8,//已确认状态
								confirmResult:'',
							}]).then(value => {
								if (value.code == 0) {
									uni.showToast({
										title: value.message,
										icon:"none",
										duration:500
									});
									setTimeout(()=>{
										page.value = CONSTANT.initPage; //重置页码
										assetsList.value = []; //重置列表
											getList()
									},500)
								} else {
									uni.showToast({
										title: value.message,
										icon:"none"
									});
								}
							})
						}else if(tipContent.value=='确认未完成？'){
							assetSign([{
								assetExecDtlId:signParams2.value.assetExecDtlId,
								version:signParams2.value.version,
								status:9,//不通过
								confirmResult:'',
							}]).then(value => {
								if (value.code == 0) {
									uni.showToast({
										title: value.message,
										icon:"none",
										duration:500
									});
									setTimeout(()=>{
										page.value = CONSTANT.initPage; //重置页码
										assetsList.value = []; //重置列表
											getList()
									},500)
								} else {
									uni.showToast({
										title: value.message,
										icon:"none"
									});
								}
							})
						}
					}
				}
				//未完成
				const noCompleteBtn = (item)=>{
					signParams2.value=item
					tipContent.value='确认未完成？'
					messageTip.value.openTip()
				}

			return {
				applyInfo,
				showLoadMore,
				loadMoreText,
				assetsList,
				searchParams,
				execBtn,
				execOutBtn,
				signBtn,
				isPermission,
				tipContent,
				messageTip,
				handleShowOrHideTip,
				noCompleteBtn
			}
		},
	})
</script>

<style scoped lang="scss">
	.assetMain {
		background-color: #eee;
		font-size: 14px;
		padding: 10px;
	}

	.assetList {
		padding: 10px;
		background-color: #fff;
		line-height: 23px;
		color: #666;
		border-radius: 5px;
	}
	.asset-title{
		padding-bottom: 10px;
		border-bottom: 1px solid #ddd;
		position: relative;
		font-weight: bold;
		font-size: 16px;
		margin-bottom: 10px;
	}

	.itemHead {
		position: relative;
	}

	.assetListItem {
		margin-top: 10px;
		background-color: #f5f5f5;
		border-radius: 5px;
	}

	.itemHeadTitle {
		text-indent: 15px;
		height: 35px;
		line-height: 35px;
		font-weight: bold;
		color: #333;
		border-bottom: 1px solid #ddd;
	}

	.itemHeadState {
		position: absolute;
		right: -10px;
		top: 0px;
		display: inline-block;
		background-color: #6f99ed;
		font-size: 11px;
		color: #fff;
		padding: 0px 15px;
		border-radius: 15px 0 0 15px;
	}

	.itemContent {
		font-size: 15px;
		box-sizing: border-box;
		padding:10px;
		// border-bottom: 1px solid #eee;
		display: flex;
		flex-wrap: row nowrap;
		position: relative;
	}

	.itemContent>view:nth-child(1) {
		width: 85%;
	}

	.itemContent>view:nth-child(1)>view {
		padding: 2px 0;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
		cursor: pointer;
	}
	
	.item-staus{
		position: absolute;
		right: 10px;
		top: 10px;
		font-size: 12px;
		border-radius: 15px;
		padding: 0px 15px;
		display: inline-block;
	}
	

	.itemHand {
		position: absolute;
		right: 10px;
		bottom: 15px;
	}

	.itemHand>view {
		display: inline-block;
		padding: 0px 23px;
		font-size: 12px;
		border-radius: 15px;
	}

	.buttons {
		padding: 10px;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-around;
		font-size: 15px;
	}

	// .buttons * {
	// 	font-size: 15px;
	// }
	
	.font-12{
		font-size: 12px;
	}
	
</style>
